<template>
<div>
  <Header :title="title"></Header>
  <van-form @submit="onSubmit">
    <van-field
        v-model="formValue.name"
        name="name"
        label="姓名"
        placeholder="请输入姓名"
        :rules="[{ required: true, /*message: '请输入姓名' */}]"
    />
    <van-field
        v-model="formValue.say"
        name="say"
        label="留言"
        type="textarea"
        rows="2"
        autosize
        maxlength="50000"
        show-word-limit
        placeholder="请输入留言"
        :rules="[{ required: true,/* message: '请输入留言'*/ }]"
    />
    <div style="margin: 16px;">
      <van-button square type="primary" block native-type="submit">提交</van-button>
    </div>
  </van-form>

</div>
</template>

<script>
import Header from "../components/Header";
import {Dialog} from "vant";
import {toSay} from "../utils/api";
import {getCookie} from "../utils/util";
const that = this
export default {
  name: "Write",
  components: {Header},
  data(){
    return {
      title: '写留言',
      formValue:{
        name: '',
        say:'',
      },
      mySayList:[

      ]
    }
  },
  created() {
    console.log('write')
    console.log(this.$store.getters.getUserInfo)
  },
  methods:{
    async onSubmit(values){
      const res = await toSay(values.name,values.say,this.$store.getters.getUserInfo.id)
      console.log(res.data)
      this.mySayList = JSON.parse(localStorage.getItem('mySayList'))
      if(this.mySayList == null){
        this.mySayList = []
      }
      this.mySayList.push(res.data)
      this.$store.commit('set_mySayList',this.mySayList)
      console.log(this.$store.getters.getMySayList)

      Dialog.alert({
        message: res.message,
      }).then(() => {
        // on close
        // this.$router.go(0)
      });
    }
  }
}
</script>

<style scoped>

</style>